package com.sawyer;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author : SawyerQiu
 * @date : 2024/9/24 15:59
 */
public class SpiderHelper {

    public static String execCmd(String command) {
        StringBuilder output = new StringBuilder();
        Process p;
        try {
            p = Runtime.getRuntime().exec(command);
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line).append("\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return output.toString();
    }

    public static void writeToFile(String fileName, StringBuilder stringBuilder) {
        String content = stringBuilder.toString();

        try {
            FileWriter writer = new FileWriter(fileName);
            writer.write(content);
            writer.flush();
            writer.close();
            System.out.println("数据写入成功！");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("数据写入文本时发生错误！");
        }
    }
}
